function sqlTempCommitBtn() {
	var parent_id = $.trim($("input[name=parentId]").val());
	var sql_category = $.trim($("#sqlCategory option:selected").val());
	var node_name = $.trim($("#sqlChildrenNodeName").val());
	var leaf_node = $.trim($(".isleaf:checked").val());
	var sql_temp_comment = $.trim($("#sqlComment").val());
	var sort = $.trim($("#sqlSort").val());
	if (node_name == null || node_name == '' || node_name == 'undefined') {
		swal("失败", "名称不能为空", "error");
		return;
	}
	var url = getRoot() + "/dataManage/insertSqlTempMenu";
	var param = {
		parentId : Number(parent_id),
		sqlCategory : Number(sql_category),
		childrenName : node_name,
		comment : sql_temp_comment,
		leaf : Number(leaf_node),
		sort : Number(sort)
	}
	$.post(url, param, function(json) {
		if (json.success) {
			swal("成功", "成功录入sql模板", "success");
			window.location.reload();
		} else {
			swal("失败", "录入失败", "error");
		}
	})
}
$(document).ready(
		function() {
			/*
			 * commitBtnNext(); typeSelected(); sqlChildrenCommitBtn();
			 */
			var url = getRoot() + "/dataManage/ajaxFindParentNode";
			$.post(url, {
				"type" : 1
			}, function(data) {
				var str = "";
				for ( var i in data) {
					var optStr = "<option value='" + data[i].id + "'>"
							+ data[i].parentName + "</option>"
					str += optStr;
				}
				$("#sqlSubType").empty();
				$("#sqlSubType").append(str);
			});
			parentRadioClick('isParent', 'parentTree');
			// redioControllerClcikEvent("isParent", "parentTree");
			redioControllerClcikEvent("isleaf", "sqlComment");
			treeSelectMenuClickEvent();
			sqltplMenuClickEvent();
		});

/*
 * function commitBtnNext() { $("#nextCommitBtn").click( function() { var url =
 * getRoot() + "/dataManage/insertParentNode"; var isExistedUrl = getRoot() +
 * "/dataManage/isExisted"; var parentName =
 * $.trim($("#sql_parent_node_name").val()); if (parentName == null ||
 * parentName == '' || parentName == 'undefined') { swal("失败", "名称不能为空",
 * "error"); return; } var type = $("#sqlInsertSelect").val();
 * $.post(isExistedUrl, { "parentName" : parentName, "type" : type },
 * function(isExisted) { if (!isExisted) { $.post(url, { "parentName" :
 * parentName, "type" : type }, function(data) { if (!data.success) { swal("失败",
 * data.message, "error"); } window.location.href = getRoot() +
 * "/dataManage/insertChildrenNode"; }); } else { swal("失败", "节点已经存在", "error"); }
 * }); }) }
 */

/*
 * function typeSelected() { $("#sqlMainType").change( function() { var url =
 * getRoot() + "/dataManage/ajaxFindParentNode"; if ($("#sqlMainType").val() ==
 * 1) { $.post(url, { "type" : 1 }, function(data) { var str = ""; for ( var i
 * in data) { var optStr = "<option value='" + data[i].id + "'>" +
 * data[i].parentName + "</option>" str += optStr; } $("#sqlSubType").empty();
 * $("#sqlSubType").append(str); }); } if ($("#sqlMainType").val() == 2) {
 * $("#sqlSubType").empty(); $("#sqlSubType").append("<option value='1'>1</option>");
 * $.post(url, { "type" : 2 }, function(data) { var str = ""; for ( var i in
 * data) { var optStr = "<option value='" + data[i].id + "'>" +
 * data[i].parentName + "</option>" str += optStr; } $("#sqlSubType").empty();
 * $("#sqlSubType").append(str); }); } }); }
 */

/*
 * function sqlChildrenCommitBtn() { $("#CommitBtn").click( function() { var
 * sqlSubType = $.trim($("#sqlSubType").val()); var sqlCategory =
 * $.trim($("#sqlCategory").val()); var sqlComment =
 * $.trim($("#sqlComment").val()); var sqlChildrenNodeName =
 * $.trim($("#sqlChildrenNodeName") .val()); if (sqlSubType == null ||
 * sqlSubType == '' || sqlSubType == 'undefined') { swal("失败", "类型不能为空",
 * "error"); return; } if (sqlCategory == null || sqlCategory == '' ||
 * sqlCategory == 'undefined') { swal("失败", "类别不能为空", "error"); return; } if
 * (sqlComment == null || sqlComment == '' || sqlComment == 'undefined') {
 * swal("失败", "内容不能为空", "error"); return; } if (sqlChildrenNodeName == null ||
 * sqlChildrenNodeName == '' || sqlChildrenNodeName == 'undefined') { swal("失败",
 * "名称不能为空", "error"); return; } var url = getRoot() +
 * "/dataManage/ajaxInsertChildrenNode"; $.post(url, { "sqlSubType" :
 * sqlSubType, "sqlCategory" : sqlCategory, "sqlComment" : sqlComment,
 * "sqlChildrenNodeName" : sqlChildrenNodeName }, function(data) { if
 * (data.success) { swal("成功", data.message, "success"); } else { swal("失败",
 * data.message, "error"); } }); }); }
 */

function redioControllerClcikEvent(redioController, target) {
	$("." + redioController).on("click", function() {
		var redioControllerValue = $(this).val();
		if (redioControllerValue == 1) {
			$("." + target).show();
		} else {
			$("." + target).hide();
		}
	})

}

function onLoadParent() {
	var _html = "<option value='0' selected='selected'>请选择父节点</option>";
	var sqlCategory = $("#sqlCategory").find("option:selected").val();
	var url = getRoot() + "/dataManage/getSqlTempByParentId";
	$.post(url, {
		pid : 0,
		sql_gategory : sqlCategory
	}, function(resp) {
		// var json = jsonDecode(resp.responseText);

		if (resp.success) {
			for (obj in resp.list) {

				var _option = "<option value='" + resp.list[obj].id + "'>"
						+ resp.list[obj].childrenName + "</option>"
				_html += _option;
			}
			$("#parentTree").html(_html);
			$("#parentTree").selectpicker('refresh');
		}
	});
	createSelect('parentTree', url, sqlCategory, 'nodeTree');
	selectChosenEvent('selecterEvent', 'parentId');
}
function createSelect(clickSelecter, url, sqlCategory, inputObject) {
	$("#" + clickSelecter)
			.on(
					"change",
					function() {
						var _html = "<option value='0' selected='selected'>请选择节点</option>";
						var selecterValue = $(this).find("option:selected")
								.val();
						$.post(url, {
							pid : selecterValue,
							sqlCategory : sqlCategory
						}, function(resp) {
							if (resp.success) {
								for (obj in resp.list) {

									var _option = "<option value='"
											+ resp.list[obj].id + "'>"
											+ resp.list[obj].childrenName
											+ "</option>";
									_html += _option;
								}
								$("#" + inputObject).html(_html);
								$("#" + inputObject).selectpicker('refresh');
								$("#" + inputObject).show();
							}
						})
						// var selecterDiv = $(this);
					})
	selectChosenEvent('selecterEvent', 'parentId');
}
function selectChosenEvent(selecterEvent, inputObject) {
	$("." + selecterEvent).on("change", function() {
		var value = $(this).find("option:selected").val();
		$("input[name=" + inputObject + "]").val(value);
	})
}

function parentRadioClick(redioController, target) {
	$("." + redioController)
			.on(
					"click",
					function() {
						var redioControllerValue = $(this).val();
						if (redioControllerValue == 1) {
							var _html = "<option value='0' selected='selected'>请选择父节点</option>";
							if ('请选择数据库类别' == $("#sqlCategory").find(
									"option:selected").html()) {
								swal("提示", "请选择数据库类别", "error");
								return false;
							} else {
								var sqlCategory = $("#sqlCategory").find(
										"option:selected").val();
								var url = getRoot()
										+ "/dataManage/getSqlTempByParentId";
								$
										.post(
												url,
												{
													pid : 0,
													sql_category : sqlCategory
												},
												function(resp) {
													if (resp.success) {
														for (obj in resp.list) {

															var _option = "<option value='"
																	+ resp.list[obj].id
																	+ "'>"
																	+ resp.list[obj].childrenName
																	+ "</option>"
															_html += _option;
														}
														$("#parentTree").html(
																_html);
														$("#parentTree")
																.selectpicker(
																		'refresh');
													}
												});
								createSelect('parentTree', url, sqlCategory,
										'nodeTree');
								selectChosenEvent('selecterEvent', 'parentId');
								$("." + target).show();
							}

						} else {
							$("." + target).hide();
						}
					})
}

function treeSelectMenuClickEvent() {
	$("#sqlShowCategory").on("change", function() {
		var sql_category = $(this).find("option:selected").val();
		var url = getRoot() + "/dataManage/getSqlTempList";
		var param = {
			pid : 0,
			sql_category : sql_category
		}
		$.post(url, param, function(json) {
			var list = json.list;
			if (list == null || list.length == 0) {
				swal("提示", "暂无数据");
			} else {
				$("#sqlTempShow").html(createMenuHtml(list, true));
			}

		});
	})
}

function createMenuHtml(htmlList, isShow) {
	if (htmlList.length == 0) {
		return "";
	}
	var _html = "<ul>";
	var isShowT = isShow ? "style='display: block;'" : "style='display: none;'";
	var html
	for (var i = 0; i < htmlList.length; i++) {
		var _temphtml = "<li class='parent_li' "
				+ isShowT
				+ "><span title='Collapse this branch' "
				+ "class='clickTree'><i class='icon-calendar icon-minus-sign'></i><b>"
				+ ""
				+ htmlList[i].childrenName
				+ "</b></span>"
				+ "<input type='hidden'  name='SqlTplMenuVOId' value='"
				+ htmlList[i].id
				+ "' />"
				+ "<input type='hidden'  name='SqlTplMenuVOSort' value='"
				+ htmlList[i].sort
				+ "' />"
				+ "<input type='hidden'  name='SqlTplMenuVOType' value='"
				+ htmlList[i].sqlCategory
				+ "' />"
				+ "<input type='hidden'  name='SqlTplMenuVOComment' value='"
				+ htmlList[i].comment
				+ "' />"
				+ "<input type='hidden'  name='SqlTplMenuVOLeaf' value='"
				+ htmlList[i].leaf
				+ "' />"
				+ "<input type='hidden'  name='SqlTplMenuVOPid' value='"
				+ htmlList[i].parentId
				+ "' />"
				+ "-----<span title='Collapse this branch' class='edit_button' style='width: auto;'><i class='icon-edit'></i><b >编辑</b> </span>"
				// + "-----<span title='Collapse this branch' style='width:
				// auto;'><i class='icon-arrow-up'></i><b
				// class='up_button'>上移</b> </span>"
				// + "-----<span title='Collapse this branch' style='width:
				// auto;'><i class='icon-arrow-down'></i><b
				// class='down_button'>下移</b> </span>"
				+ "-----<span title='Collapse this branch' style='width: auto;'><i class='icon-minus-sign'></i><b class='add_button'>新建子节点</b> </span>"
				+ "-----<span title='Collapse this branch' style='width: auto;'><i class='icon-minus-sign'></i><b class='delete_button'>删除菜单</b> </span>"
				+ "" + createMenuHtml(htmlList[i].sqlTplMenuVOs, false)
				+ "</li>";

		_html += _temphtml;
	}
	_html += "</ul>";
	return _html;
}
function sqltplMenuClickEvent() {
	$('.tree li:has(ul)').addClass('parent_li').find(' > span').attr('title',
			'Collapse this branch');
	$(document).on('click', '.clickTree', function(e) {
		var children = $(this).parent('li.parent_li').find(' > ul > li');
		if (children.is(":visible")) {
			children.hide('fast');
			/*
			 * $(this).attr('title', 'Expand this branch') .find(' >
			 * i').first().addClass('icon-plus-sign')
			 * .removeClass('icon-minus-sign');
			 */
		} else {
			children.show('fast');
			/*
			 * $(this).attr('title', 'Collapse this branch').find( ' >
			 * i').first().addClass('icon-minus-sign')
			 * .removeClass('icon-plus-sign');
			 */
		}
		e.stopPropagation();
	});
	buttonClickEvent();
}
function buttonClickEvent() { 
	// 新建
	$(document).on(
			"click",
			".add_button",
			function() {
				var parentId = $(this).parent("span").siblings(
						"input[name=SqlTplMenuVOId]").val();
				var sqlType = $(this).parent("span").siblings(
						"input[name=SqlTplMenuVOType]").val();
				$("#sqltemp-edit-modal").find(".parentNodeChosen").hide();
				$("#sqltemp-edit-modal").find("input[name=sqlTempParentId]")
						.val(parentId);
				$("#sqltemp-edit-modal").find("#sqlTempCategory")
						.find("option").each(function() {
							if ($(this).val() == sqlType) {
								$(this).attr("selected", true);
							}
						})
				$("#sqltemp-edit-modal").find("#sqlTempCategory").selectpicker(
						'refresh');
				redioControllerClcikEvent('sqlTempleaf', 'sqlTempComment');
				$("#sqltemp-edit-modal").modal("show");
				showDialog('add');
			});

	// 修改
	$(document).on(
			"click",
			".icon-edit",
			function() {
				var Id = $(this).parent("span").siblings(
						"input[name=SqlTplMenuVOId]").val();
				var sqlType = $(this).parent("span").siblings(
						"input[name=SqlTplMenuVOType]").val();
				var comment = $(this).parent("span").siblings(
						"input[name=SqlTplMenuVOComment]").val();
				var sqlNodeName = $(this).parent("span").siblings(".clickTree")
						.find("b").html();
				
				var sqlNodePid = $(this).parent("span").siblings("input[name=SqlTplMenuVOPid]").val();
				var sqlTempLeaf = $(this).parent("span").siblings("input[name=SqlTplMenuVOLeaf]").val();
				var sqlTempSort = $(this).parent("span").siblings("input[name=SqlTplMenuVOSort]").val();
				$("#sqltemp-edit-modal").find(".parentNodeChosen").hide();
				if($("input[name=SqlTplMenuVOLeaf]").val()=="1"){
					$(".sqlTempComment").show();
				}
				$("#sqlTempName").val(sqlNodeName);
				$("input[name=sqlTempId]").val(Id);
				$("#sqlTempComment").val(comment);
				$("#sqlTempCategory").find("option").each(function() {
							if ($(this).val() == sqlType) {
								$(this).attr("selected", true);
							}
						});
				$("input[name=sqlTempParentId]").val(sqlNodePid);
				$("#sqlTempleaf").val(sqlTempLeaf);
				$("#sqlTempSort").val(sqlTempSort);
				if(sqlTempLeaf=="1"){
					$(".sqlTempComment").show();
				}
				$("#sqlTempCategory").selectpicker(
						'refresh');
				redioControllerClcikEvent('sqlTempleaf', 'sqlTempComment');
				$("#sqltemp-edit-modal").modal("show");
				showDialog('edit');
			});

	// 删除
	$(document).on(
			"click",
			".delete_button",
			function() {
				var childrenNode = $(this).parent("span").siblings("ul").find(
						"li");
				console.log(childrenNode);
				if (childrenNode.length > 0) {
					swal("提示", "该节点下有子节点，不能删除");
					return false;
				}
				swal({
					title : "确定删除?",
					text : "你将删除该节点!",
					type : "warning",
					showCancelButton : true,
					confirmButtonColor : "#DD6B55",
					confirmButtonText : "是, 删除!",
					cancelButtonText : "不删除!",
					closeOnConfirm : false,
					closeOnCancel : false
				}, function(isConfirm) {
					if (isConfirm) {
						$.post(getRoot() + "/dataManage/removeSqlTpl", {
							id : Number($(this).parent("span").siblings(
									"input[name=SqlTplMenuVOId]").val())
						}, function(json) {
							if (json.success) {
								swal("成功", "操作成功", "success");
								window.location.reload();
							} else {
								swal("失败", "操作失败", "error");
							}
						});
					} else {
						swal("取消删除", "该节点未删除 :)", "error");
					}
				});

			})
}
function showDialog(model) {

	var url = "edit" == model ? getRoot() + "/dataManage/updateSqlTempMenu"
			: getRoot() + "/dataManage/insertSqlTempMenu";

	$(document).on("click", "#SqlTempCommitBtnTemp", function() {
		var parent_id = $.trim($("input[name=sqlTempParentId]").val());
		var sql_category = $.trim($("#sqlTempCategory option:selected").val());
		var node_name = $.trim($("#sqlTempName").val());
		var leaf_node = $.trim($("#sqlTempleaf").val());
		var sql_temp_comment = $.trim($("#sqlTempComment").val());
		var sort = $.trim($("#sqlTempSort").val());
		if (node_name == null || node_name == '' || node_name == 'undefined') {
			swal("失败", "名称不能为空", "error");
			return;
		}
		var param = {
			parentId : Number(parent_id),
			sqlCategory : Number(sql_category),
			childrenName : node_name,
			comment : sql_temp_comment,
			leaf : Number(leaf_node),
			sort : Number(sort)
		}
		if ("edit" == model) {
			param.id = $.trim($("input[name=sqlTempId]").val());
		}
		
		$.post(url, param, function(json) {
			if (json.success) {
				swal("成功", "操作成功", "success");
				window.location.reload();
			} else {
				swal("失败", "操作失败", "error");
			}
		})
	})

}